<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class ActivatePage {

	private $alerter;
	private $access;
	private $item_functions;
	private $toolbox;
	private $error_nav;
	private $db_access;
	
	public function __construct() {
		$this->access = new AccessManager();
		$this->alerter = new Alerter();
		$this->item_functions = new ItemFunctions();
		$this->toolbox = new Toolbox();
		$this->error_nav = new ErrorNavigation();
		$this->db_access = new DbAccessor();
	}

	public function set_access_manager($access) {
		$this->access = $access;
	}

	public function set_item_functions($item_functions) {
		$this->item_functions = $item_functions;
	}

	public function set_toolbox($toolbox) {
		$this->toolbox = $toolbox;
	}

	public function set_alerter($alerter) {
		$this->alerter = $alerter;
	}

	public function set_error_navigation($error_nav) {
		$this->error_nav = $error_nav;
	}

	public function set_db_accessor($db_access) {
		$this->db_access = $db_access;
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		$this->access->check_access("VIEW");
		if ($_SESSION['user_id']) {
			$this->toolbox->endpage($page, "already registered");
		}
	}


	function page_inc_execute_action($action) {
		switch($action) {
			default:
				$this->alerter->add_alert("unknown action: $action");
				break;
		}
	}


	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {
		global $CONFIG;

		if (!$_GET['code'] or !$page->id) {
			$this->error_nav->errorpage(9);
		}

		$sqlresult = $this->db_access->db_do_query(
				$this->db_access->db_query_select(
						array(
								array('*')), 
						array("MEMBER"), 
						ereg("^[0-9]+$", $page->id) ? "user_id = {$page->id}" : "nick = '{$page->id}'")
				);
		
		if ($this->db_access->db_num_rows($sqlresult) != 1) {
			$this->error_nav->errorpage(9);
		}
			
		$user = $this->db_access->db_fetch_array($sqlresult);
		
		if ($_GET['code'] == ("ACTIV" . md5("{$user['nick']}{$user['email']}blablabla6"))) {
		
			$this->db_access->db_do_query($page, 
					$this->db_access->db_query_update(
							array('member_since', 'title'), 
							array('member_since', '"boomdraak"'), 
							"MEMBER", 
							"user_id = {$user['user_id']}"));
			
			$this->login_manager->login($user['user_id']);

			$this->toolbox->endpage("OK, je registratie is nu compleet. Veel plezier op de site, en vul bijtijds ook je <a href=/service/.profile?action=edit>profiel</a> verder in (je wordt er automatisch naar dooverwezen zodadelijk). <script language=JavaScript>setTimeout(\"location.href='/service/.profile?action=edit'\", 5000);</script>");
		
		} else {
			$this->error_nav->errorpage(12);
		}
	}

	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress() {}
}

?>